package cn.lg.soar.general.controller;

import cn.lg.soar.common.data.tree.TreeOption;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.general.api.constant.ModuleConst;
import cn.lg.soar.general.entity.Region;
import cn.lg.soar.general.service.IRegionService;
import cn.lg.soar.mvc.annotations.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Api(tags = "区域管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/region")
public class RegionController extends BaseController<Region, IRegionService> {

    @Override
    protected Serializable getId(Region entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "区域管理";

    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Validated Region data) {
        return super.createResult(data);
    }

    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated Region data) {
        return super.updateResult(data);
    }

    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @DetailMapping
    public HttpResult<Region> detail(@Id Long id) {
        return super.detailResult(id);
    }

    @QueryMapping
    public HttpResult<PageResult<Region>> query(PageQuery pageQuery, QueryParams params){
        return super.queryResult(pageQuery, params);
    }

    @ExportMapping
    public void exports(QueryParams params, HttpServletResponse resp){
        super.exportExcel(RegionController.SUBJECT, params, resp);
    }

    @ImportMapping
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @ExistMapping
    public HttpResult<Boolean> exist(QueryParams params){
        return super.existResult(params);
    }

    @ApiOperation("树形数据")
    @GetMapping("tree")
    public HttpResult<List<TreeOption<Integer>>> tree() {
        return HttpResult.data(service.tree());
    }

}
